package com.ndp.fb.walle.controller;
import com.alibaba.fastjson.JSON;
import com.ndp.fb.enums.ErrorType;
import com.ndp.fb.rdb.model.Organization;
import com.ndp.fb.rdb.model.User;
import com.ndp.fb.walle.business.OrganizationService;
import com.ndp.fb.walle.model.vo.OutputResult;
import com.ndp.fb.walle.util.ResponseUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by hermen.he on 2016/2/25.
 */
@RequestMapping("/organization")
@Controller
public class OrganizationController {

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private OrganizationService organizationService;

    @RequestMapping(value = "saveOrganization")
    public ResponseEntity<String> saveOrganization(HttpServletRequest request, @RequestBody Organization organization) {
        logger.info("OrganizationController saveOrganization() begin.");
        ErrorType errorType = null;
        Organization data = null;
        try {
           // Organization organization = JSON.parseObject(param,Organization.class);
            User user = (User) request.getSession().getAttribute("user");
            Organization o = organizationService.findById(user.getOrgId());
            if(o == null){
                //保存,在我们目前系统中，是不会有这种情况的，因为是先有新公司信息才有管理员的。
                organizationService.save(organization);
            }else{
                //修改
                organization.setId(user.getOrgId());
                organizationService.modify(organization);
            }
            data = organization;
        } catch (Exception e) {
            errorType = ErrorType.INTERNAL_EXCEPTION;
            logger.error("保存/修改组织出现异常", e);
        }
        OutputResult outputResult = ResponseUtils.bulidOutputResult(errorType, data);
        logger.info("OrganizationController saveOrganization() end.");
        return ResponseUtils.returnJsonWithUTF8(JSON.toJSONString(outputResult));
    }
    @RequestMapping(value = "searchOrganization")
    @ResponseBody
    public ResponseEntity<String> findByUser(HttpServletRequest request) {
        logger.info("OrganizationController findByUser() begin.");
        ErrorType errorType = null;
        Organization organization = null;
        try {
            User user = (User) request.getSession().getAttribute("user");
            organization = organizationService.findById(user.getOrgId());
        } catch (Exception e) {
            errorType = ErrorType.INTERNAL_EXCEPTION;
            logger.error("查询组织出现异常", e);
        }
        OutputResult outputResult = ResponseUtils.bulidOutputResult(errorType, organization);
        logger.info("OrganizationController findByUser() end.");
        return ResponseUtils.returnJsonWithUTF8(JSON.toJSONString(outputResult));
    }
}
